import { Table, message } from 'antd';
import Cls from 'classnames';
import { useContext, useEffect, useMemo } from 'react';
import { contractModalColumns } from '../mconfig/UpdateContractColumns';
import { IToAssetControllerActionEnum } from './useManageReducer';
import { ToAssetControllerContext } from './useManageContext';

export const UpdateContractView: React.FC<any> = ({ handleSelectContract, pageType, contractForm }) => {
  const { contractList, materielList, toAssetControllerDispatch } = useContext(ToAssetControllerContext);

  /** 取消选中的合同 */
  const cancelContractList = (record: Record<string, any>) => {
    let idx = contractList.findIndex((item: Record<string, any>) => {
      return item.id == record.id;
    });
    if (idx !== -1) {
      contractList.splice(idx, 1);
      toAssetControllerDispatch({
        type: IToAssetControllerActionEnum.SELECT_CONTRACT,
        payload: [...contractList],
      });
    }
  };

  /** 修改合同列表的数据 */
  const changeContractData = (obj: any) => {
    let tempList = [...contractList];
    /** 修改合同列表当前项的金额 */
    if (obj.type == 'effectiveMoney') {
      tempList[obj.index].effectiveMoney = obj.value;
    }
    /** 存储到仓库合同列表 */
    toAssetControllerDispatch({
      type: IToAssetControllerActionEnum.SELECT_CONTRACT,
      payload: tempList,
    });
  };

  /** 表格行配置 */
  const columns: Record<string, any>[] = useMemo(() => {
    return contractModalColumns(cancelContractList, changeContractData, handleSelectContract, pageType);
  }, [contractList]);

  return (
    <div className={Cls('list-table', 'descriptionsType-tableSty')}>
      <Table bordered size="small" dataSource={contractList} columns={columns} rowKey={(res) => res?.id} scroll={{ x: 1200, y: '' }} pagination={false} />
    </div>
  );
};
